<?php

/**
 * Created by Sven.
 * Date: 2016/12/25
 * Time: 13:00
 * 实体基类
 */
class Model {
    protected  $_table; // 该模型对应的表名

    /**
     * 获取下一条即将插入的记录id值
     * @param $_table 表名
     * @return mixed 下一条自动增长的id值
     */
    public function nextAutoIncrementId($_table){
        $_sql = "show table STATUS where name='$_table'";
        return self::one($_sql)->Auto_increment;
    }

    /**
     * 查询单条记录
     * @param $_sql
     * @return mixed 查询出的第一条结果数组
     */
    public function one( $_sql ) {
        $_mysqli = DB::getDB();
        $_result = $_mysqli->query( $_sql );

        // 以对象形式取出数据集 => array( object1. object2, object3 ... );
        $_object = Tool::htmlString($_result->fetch_object());
        // 释放$_result, $_mysqli
        DB::unDB( $_result, $_mysqli );
        return $_object;
    }

    /**
     * 执行add update delete语句
     * @param $_sql
     * @return bool 是否执行成功
     */
    public function execute( $_sql ) {
        $_mysqli = DB::getDB();
        $_mysqli->query( $_sql );
        $_affected_rows = $_mysqli->affected_rows;

        if ( $_affected_rows <= 0 ) {
            if ( $_mysqli->errno ) {
                exit( '执行SQL语句错误：' . $_mysqli->error );
            }
            return false;
        }
        return true;
    }

    /**
     * 查询多条记录
     * @param $_sql
     * @return array 多条记录组成的结果集
     */
    public function query($_sql){
        $_mysqli = DB::getDB();

        $_result = $_mysqli->query( $_sql );

        // 以对象形式取出数据集 => array( object1. object2, object3 ... );
        $_objects = array();
        while ( !!$row = $_result->fetch_object() ) {
//        while (!!$_html[] = $_result->fetch_assoc()){
            $_objects[] = Tool::htmlString($row);
        }
        // 释放$_result, $_mysqli
        DB::unDB( $_result, $_mysqli );
        return $_objects;
    }

    /**
     * 执行多条update add 的语句
     * @param $_sql
     */
    public function multi_query($_sql){
        $_mysqli = DB::getDB();
        $_mysqli->multi_query( $_sql );
    }

    /**
     * 铜鼓id获取总条数
     * @param $_sql
     * @return mixed 数量
     */
    public function getTotalCount($_table){
        $_sql = "SELECT 
                                  count(1) as count 
                                FROM  
                                  $_table
                          WHERE  
                                1=1 ";

        $_mysqli = DB::getDB();
        $_result = $_mysqli->query( $_sql );
        $_count  = $_result->fetch_object()->count;
        return $_count;
    }


    /**
     * 查询满足字段的记录是否存在
     * @param $_table
     * @param string $_field
     * @param int $_field_value
     * @return mixed
     */
    public function getCount($_table,  $_field = 'id', $_field_value = 0){
        $_sql = "SELECT 
                                  count(1) as count 
                                FROM  
                                  $_table
                          WHERE  
                                $_field= '$_field_value' ";

        return self::one($_sql)->count;
    }

    public function getOne($_table, $_field = 'id', $_field_value = '0'){
        $_sql = "SELECT 
                                 *  
                                FROM  
                                  $_table
                          WHERE  
                                $_field= '$_field_value'
                                 LIMIT   1; ";

        return self::one($_sql);
    }

    public function delete($_table, $_field = 'id', $_field_value = '0'){
        $_sql = "DELETE 
                                    FROM $_table 
                        WHERE  
                                      $_field = '$_field_value' ";

        return self::execute($_sql);

    }

}
